package com.so206.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.so206.mapper.*;
import com.so206.mapper.ext.SysUserVipMapper;
import com.so206.po.*;
import com.so206.po.ext.SysUserExt;
import com.so206.po.ext.SysUserVipExt;
import com.so206.service.UserService;
import com.so206.utils.Base64;
import com.so206.utils.BeanCheck;
import com.so206.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private SystemUserMapper userMapper;

    @Autowired
    private SystemConfigMapper configMapper;

    @Autowired
    private SystemWebMapper webMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysUserVipMapper vipMapper;

    @Autowired
    private SystemVipMapper systemVipMapper;


    @Override
    public SystemUser findUserByUserNameAndPassword(SystemUser user) {
        Integer lck = 2;
        List<SystemUser> users = userMapper.selectByUserIsLocked(user.getUser(), lck);
        if (users != null && users.size() > 0) {
            SystemUser systemUser = users.get(0);
            String pwd = systemUser.getPwd();
            systemUser.setPwd(MD5Util.MD5Encode(pwd, "utf-8"));
            return systemUser;
        }
        return null;
    }

    @Override
    public Long saveUser(SystemUser user) {
        SystemWeb web = webMapper.selectByPrimaryKey(1L);
        String str = UUID.randomUUID().toString() + UUID.randomUUID().toString();
        String s = Base64.encode(str.getBytes(StandardCharsets.UTF_8));
        user.setAppkey(s.substring(10, 42));
        user.setBalnes(new BigDecimal("0.00"));
        if (user.getZspaytype() == null) {
            user.setZspaytype(2);
        }
        if (user.getZsname() == null) {
            user.setZsname("请填写!");
        }
        if (user.getZspayid() == null) {
            user.setZspayid("请填写!");
        }
        user.setLastLoginIp("0.0.0.0");
        user.setIsAutoSett(1);
        user.setIsLocked(2);
        userMapper.insert(user);
        Long id = user.getId();
        SystemConfig config = new SystemConfig();
        config.setUid(id);
        config.setCopy("206云支付");
        config.setDescription("24小时在线收款系统");
        config.setKeywords("24小时在线收款系统");
        config.setLiuyan("谢谢合作!");
        config.setPanel("24小时在线收款系统");
        config.setGg("欢迎使用24小时在线收款系统.");
        config.setKfqq("000000000");
        config.setMusic("118.mp3");
        config.setYm(web.getHttpUrl());
        config.setSitename("XXX24小时在线收款系统");
        configMapper.insert(config);
        return id;
    }

    @Override
    public PageInfo<SysUserExt> findByPage(Integer page, Integer rows, String user, Long pid, Long role, String email, Long parent_id) {
        PageInfo<SysUserExt> bean = new PageInfo<>();
        PageHelper.startPage(page, rows);
        List<SystemUser> list = userMapper.selectByUserIdRoleEmailUserParent(user, pid, role, email, parent_id);
        PageInfo<SystemUser> info = new PageInfo<>(list);
        info.setList(null);
        BeanCheck.copyPropertiesIgnoreNull(info, bean);
        ArrayList<SysUserExt> objects = new ArrayList<>();
        for (SystemUser systemUser : list) {
            SysUserExt ext = new SysUserExt();
            BeanCheck.copyPropertiesIgnoreNull(systemUser, ext);
            SysRole sysRole = roleMapper.selectByPrimaryKey(ext.getRole());
            ext.setRoleName(sysRole.getRoleName());
            objects.add(ext);
        }
        bean.setList(objects);
        return bean;
    }

    @Override
    public SystemUser findUserById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateUser(SystemUser user) {
        SystemUser model = userMapper.selectByPrimaryKey(user.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(user, model);
            userMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public void deleteUserById(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SystemUser> find_user_list() {
        return userMapper.selectByUserIsLocked(null, null);
    }

    @Override
    public SystemUser findUserByEmail(String email) {
        List<SystemUser> systemUsers = userMapper.selectByEmail(email);
        if (systemUsers != null && systemUsers.size() > 0) {
            return systemUsers.get(0);
        }
        return null;
    }

    @Override
    public List<SystemUser> find_user_list2(Long utype) {
        return userMapper.selectByUserParent(utype);
    }

    @Override
    public SystemUser find_user_by_email(String email) {
        List<SystemUser> users = userMapper.selectByEmail(email);
        if (users != null && users.size() > 0) {
            SystemUser systemUser = users.get(0);
            String pwd = systemUser.getPwd();
            systemUser.setPwd(MD5Util.MD5Encode(pwd, "utf-8"));
            return systemUser;
        }
        return null;
    }

    @Override
    public PageInfo<SysUserVipExt> find_by_page(Integer page, Integer rows, Long uid, Integer status, String name, String email) {
        PageHelper.startPage(page, rows);
        if (name != null && name.trim().length() == 0) {
            name = null;
        }
        if (email != null && email.trim().length() == 0) {
            email = null;
        }
        List<SysUserVipExt> list = vipMapper.find_vip_with_user(uid, status, name, email);
        return new PageInfo<>(list);
    }

    @Override
    public SysUserVipExt vip_find_by_id(Long id) {
        return vipMapper.find_by_id(null, id);
    }

    @Override
    public void delete_vip_by_id(Long id) {
        systemVipMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updaate_vip_by_id(SystemVip vip) {
        SystemVip model = systemVipMapper.selectByPrimaryKey(vip.getId());
        if (model != null) {
            BeanCheck.copyPropertiesIgnoreNull(vip, model);
            model.setOptTime(new Date());
            systemVipMapper.updateByPrimaryKey(model);
        }
    }

    @Override
    public SystemUser findUserByPhone(String phone) {
        List<SystemUser> users = userMapper.selectByPhone(phone);
        if (users != null && users.size() > 0) {
            SystemUser systemUser = users.get(0);
            String pwd = systemUser.getPwd();
            systemUser.setPwd(MD5Util.MD5Encode(pwd, "utf-8"));
            return systemUser;
        }
        return null;
    }


}
